<template>
  <div>
    <el-button slot="reference" @click="showTable">创建检查活动</el-button>
    <el-table :data="tableData">
      <el-table-column prop="checkTime" label="日期" width="180" :formatter="formatDate">
      </el-table-column>
      <el-table-column label="姓名" width="180">
        <template slot-scope="scope">
          {{adminName}}
        </template>
      </el-table-column>
      <el-table-column label="楼栋" width="180">
        <template slot-scope="scope">
          {{buildingName}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button
              size="small"
              type="primary"
              @click="toActivity(scope.row)">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total=totalNum>
      </el-pagination>
    </div>
    <el-dialog title="创建卫生检查" :visible.sync="dialogFormVisible">
      <el-form >
        <el-form-item prop="date" label="日期:" width="140">
          <div class="block">
            <el-date-picker
                v-model="newActivity.checkTime"
                align="right"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd HH:mm:ss"
                >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item prop="student" label="姓名:" width="120">
          {{newActivity.name}}
        </el-form-item>
        <el-form-item prop="building" label="楼栋:" width="120">
          {{newActivity.building}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="createdActivity">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getBuildingName, insertActivity, getAllActivity} from '../../../api/dormitory_admin'
export default {
  data () {
    return {
      pageNo: 1,
      pageSize: 5,
      totalNum: 0,
      adminName: JSON.parse(localStorage.getItem('user')).name,
      buildingName: null,
      newActivity: {
        checkTime: null,
        building: null,
        name: JSON.parse(localStorage.getItem('user')).name
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      DOT: {
        pageNo: 1,
        pageSize: 5,
        data: {
          ID: null
        }
      },
      selectForm: {
        building: '',
        inRoom: ''
      },
      tableData: []
    }
  },
  methods: {
    formatDate (row) {
      const date = new Date(row.checkTime)
      const beijingTime = new Date(date.getTime())
      const year = beijingTime.getFullYear()
      const month = (beijingTime.getMonth() + 1).toString().padStart(2, '0')
      const day = beijingTime.getDate().toString().padStart(2, '0')
      return `${year}年${month}月${day}日`
    },
    toActivity (row) {
      this.$router.push({path: '/dormitory_admin/hygieneActivity', query: {checkTime: row.checkTime}})
    },
    handleCurrentChange (newPage) {
      this.DOT.pageNo = newPage
      this.pageNo = newPage
      this.requireAllActivity()
    },
    showTable (row) {
      this.dialogFormVisible = true
    },
    requireAllActivity () {
      this.DOT.data.ID = JSON.parse(localStorage.getItem('user')).id.toString()
      getAllActivity(this.DOT).then(res => {
        this.tableData = res.data
        this.totalNum = res.otherData.totalNum
      })
    },
    createdActivity () {
      if (this.newActivity.checkTime === '' || this.newActivity.checkTime === null) {
        this.$message({
          message: '请选择日期',
          type: 'warning'
        })
      } else {
        console.log(this.newActivity.checkTime)
        let obj = {
          dormitoryAdminID: JSON.parse(localStorage.getItem('user')).id.toString(),
          checkTime: this.newActivity.checkTime
        }
        insertActivity(obj).then(res => {
          this.$message({
            message: '创建成功',
            type: 'success'
          })
          this.requireAllActivity()
          this.dialogFormVisible = false
        })
      }
    }
  },
  created () {
    let obj = {
      ID: JSON.parse(localStorage.getItem('user')).id
    }
    getBuildingName(obj).then(res => {
      this.buildingName = res.data.name
      this.newActivity.building = res.data.name
      localStorage.setItem('buildingName', JSON.stringify(res.data))
    })
    this.requireAllActivity()
  }
}
</script>
<style scoped>
</style>
